package com.yubest.designpattern.behavior.chain;

import lombok.Getter;

/**
 * 处理链
 *
 * @Author: hweiyu
 * @Date: 2022/10/26 16:15
 */
public class HandlerChain {

    @Getter
    private AbstractHandler handler;

    public HandlerChain(AbstractHandler ... handlers) {
        if (null != handlers) {
            AbstractHandler top = handlers[0];
            AbstractHandler pre = top;
            for (int i = 1; i < handlers.length; i++) {
                pre.nextHandler(handlers[i]);
                pre = handlers[i];
            }
            this.handler = top;
        }
    }

    public void handle(RequestObject object) {
        if (null != getHandler()) {
            getHandler().handle(object);
        }
    }
}
